package com.fhz.shop.ui.cart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fhz.shop.R;
import com.fhz.shop.entity.UserCart;
import com.fhz.shop.util.ImageLoader;

import java.util.List;

/**
 * 时间:2023/8/30
 *
 * @author Mr.Feng
 * 简述: 购物车中的数据布局
 */
public class CartFragmentRvItemAdapter extends RecyclerView.Adapter<CartFragmentRvItemAdapter.ViewHolder> {


    private Context mContext;
    private List<UserCart> mUserCartList;

    public CartFragmentRvItemAdapter(Context context, List<UserCart> userCartList) {
        mContext = context;
        mUserCartList = userCartList;
    }

    @NonNull
    @Override
    public CartFragmentRvItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.cart_item_rv_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate,mOnItemClickListener,mOnSubItemClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CartFragmentRvItemAdapter.ViewHolder holder, int position) {
        UserCart userCart = mUserCartList.get(position);
        holder.num.setText(String.valueOf(userCart.getCarNum()));
        ImageLoader.load(mContext,userCart.getCarImage(),holder.mhead);
        holder.mTitle.setText(userCart.getCarTitle());
        holder.mPrice.setText(String.valueOf(userCart.getCarPrice()));

        holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                userCart.setCheck(isChecked);
                mUserCartList.set(position,userCart);
            }
        });


    }

    //局部刷新
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        for (Object payload : payloads) {
            if(payload instanceof Integer){
                holder.num.setText(String.valueOf(payload));
            }else if(payload instanceof Boolean){
                holder.mCheckBox.setVisibility(View.VISIBLE);
            }
        }

    }


    @Override
    public int getItemCount() {
        return mUserCartList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private CheckBox mCheckBox;
        private TextView mTitle;
        private ImageView mhead;
        private TextView mPrice;
        private ImageView minus;
        private ImageView add;
        private TextView num;


        private OnItemClickListener mOnItemClickListener;
        private OnSubItemClickListener mOnSubItemClickListener;
        public ViewHolder(@NonNull View itemView,OnItemClickListener onItemClickListener,OnSubItemClickListener onSubItemClickListener) {
            super(itemView);
            mOnItemClickListener = onItemClickListener;
            mOnSubItemClickListener = onSubItemClickListener;
            itemView.setOnClickListener(this);
            mCheckBox = itemView.findViewById(R.id.goods_item_radio);
            mTitle = itemView.findViewById(R.id.goods_item_title);
            mhead = itemView.findViewById(R.id.goods_item_photo);
            mPrice = itemView.findViewById(R.id.goods_item_price);
            minus = itemView.findViewById(R.id.goods_item_minus);
            add = itemView.findViewById(R.id.goods_item_add);
            num = itemView.findViewById(R.id.goods_item_num);

            add.setOnClickListener(v -> {
                mOnSubItemClickListener.onSubItemClick(add.getId(),getLayoutPosition());
            });
            minus.setOnClickListener(v -> {
                mOnSubItemClickListener.onSubItemClick(minus.getId(),getLayoutPosition());
            });
            mCheckBox.setOnClickListener(v ->{
                mOnSubItemClickListener.onSubItemClick(mCheckBox.getId(),getLayoutPosition());
            });
        }

        @Override
        public void onClick(View v) {
            mOnItemClickListener.onClick(v,getLayoutPosition());
        }
    }

    public interface OnItemClickListener{
        void onClick(View view,int position);
    }
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnSubItemClickListener{
        void onSubItemClick(int id,int position);
    }

    private OnSubItemClickListener mOnSubItemClickListener;

    public void setOnSubItemClickListener(OnSubItemClickListener onSubItemClickListener) {
        mOnSubItemClickListener = onSubItemClickListener;
    }
}
